package com.rexense.beacon.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Objects;

@Data
@Builder
@AllArgsConstructor
public class Msg implements Serializable, Comparable<Msg> {

    private String msgId;

    private String labelId;

    private String beaconId;

    private double rssi;

    public String key() {
        return this.getLabelId() + "#" + this.getMsgId();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Msg msg = (Msg) o;
        return labelId.equals(msg.getLabelId())
                && msgId.equals(msg.getMsgId())
                && beaconId.equals(msg.beaconId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(labelId, msgId, beaconId);
    }

    public Msg() {
    }

    public MsgKey toMsgKey() {
        return new MsgKey(key(), System.currentTimeMillis());
    }

    @Override
    public int compareTo(Msg o) {
        return rssi > o.rssi ? -1 : 1;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MsgKey implements Serializable {

        public String msgKey;

        public long timestamp;

    }

    public static class MsgIdComparator implements Comparator<MsgKey> {

        @Override
        public int compare(MsgKey o1, MsgKey o2) {
            return Long.compare(o1.timestamp, o2.timestamp);
        }
    }

    public static class RssiComparator implements Comparator<Double> {
        @Override
        public int compare(Double o1, Double o2) {
            return Double.compare(o1, o2);
        }
    }


}
